home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 April: Mac OS SDK / Dev.CD Apr 00 SDK1.toast / Development Kits / Mac OS / Apple Guide / Engineering / Context Check Modules / IsNamedWindowsRect CC / Source / winRect.c next >
Encoding:
C/C++ Source or Header  |  1994-04-20  |  3.1 KB  |  151 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        winRect.c
  3.  
  4.     Contains:    AppleGuide Context Check
  5.  
  6.     Written by:    Josh Jacobs, Panoramic Software
  7.                 
  8.     Copyright:    ©1993 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.         1    04/16/94    JJ        First version of code
  13. */
  14.  
  15. #include    "winRect.h"
  16.  
  17. //    prototypes
  18. WindowPtr     GetWindowPtr(Str255 winName);
  19. Boolean        DoesWindowsRectMatch(WindowPtr wp, Rect* rPtr);
  20. OSErr         SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize);
  21. void        PStrCat (Str255  destination, Str255  source);
  22. void        RectToString(Rect *aRect, Str255 s);
  23. Boolean        isPressed(unsigned short k);
  24.  
  25. pascal OSErr
  26. main(WinRectData* msg, Size inSize, void* outMessage,Size* outSize, Handle ignoreMe )
  27. {
  28.     OSErr        err     =    errAECorruptData;    //    the default
  29.     Boolean        result     =     false;                //    in case we have an error we can jusr return false
  30.     WindowPtr    wPtr     =     nil;
  31.     
  32.     wPtr = GetWindowPtr(msg->winName);
  33.     if (wPtr)
  34.         result = DoesWindowsRectMatch(wPtr,(Rect*)&(msg->compareTo));
  35.  
  36.     err    = SetContextResult(&result, sizeof(Boolean), outMessage, outSize);
  37.     return(err);
  38. }
  39.  
  40.  
  41. //    GetWindowPtr returns a pointer to the author specified window
  42. WindowPtr GetWindowPtr(Str255 winName)
  43. {
  44.     WindowPeek        wp;
  45.     Str255            title;
  46.     WindowPtr        wPtr    =    nil;
  47.  
  48.     //    if it is the front window we do not need to walk the window list
  49.     if (!IUEqualString("\pFront",winName))
  50.         return(FrontWindow());
  51.         
  52.     for (wp = (WindowPeek)WindowList; wp; wp = wp->nextWindow)
  53.     {
  54.         GetWTitle((WindowPtr)wp, title);
  55.         
  56.         // if window title is "Desktop" ( == 0) pretend we didn't see it, else compare for this window
  57.         if( IUEqualString(title, "\pDesktop") != 0 )
  58.             if (!IUEqualString(title, winName))
  59.             {
  60.                 wPtr = (WindowPtr)wp;
  61.                 break;
  62.             }
  63.     }
  64.  
  65.     return(wPtr);
  66. }
  67.  
  68. Boolean    DoesWindowsRectMatch(WindowPtr wp, Rect* rPtr)
  69. {
  70.     Str255        debug;
  71.  
  72. /*    ••Only if we are doing a debug build, you do not want this code to ship!
  73.     if( isPressed(56))    //    the shift key
  74.     {
  75.         RectToString((&(((WindowPeek)wp)->port.portRect)),debug);
  76.         DebugStr(debug);
  77.     }
  78. */
  79.     return(EqualRect((&(((WindowPeek)wp)->port.portRect)),rPtr));
  80. }
  81.  
  82.  
  83. //    Setup the result the way Reno expects it
  84. OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize)
  85. {
  86.     Ptr    p;
  87.     
  88.     if (p = NewPtr(theSize))
  89.     {
  90.         BlockMove(theData, p, theSize);
  91.         
  92.         *outSize        =    theSize;
  93.         *outMessage    =    p;
  94.         
  95.         return(noErr);
  96.     }
  97.     else
  98.         return(MemError());
  99. }
  100.  
  101.  
  102. void    PStrCat (Str255  destination, Str255  source)
  103. {
  104.     short        index, t1, t2, max, newlen;
  105.     
  106.     t1 = destination[0] ;
  107.     t2 = source[0] ;
  108.     
  109.     if (t1 + t2 > kMaxStringSize)
  110.     {
  111.         newlen = kMaxStringSize;
  112.         max = kMaxStringSize - t1;
  113.     }
  114.     else
  115.     {
  116.         newlen = t1 + t2;
  117.         max = t2;
  118.     }
  119.  
  120.     for (index = 1; index <= max; index ++)
  121.         destination[index + t1] = source[index];
  122.         
  123.     destination[0] = newlen;
  124. }
  125.  
  126.  
  127. void        RectToString(Rect *aRect, Str255 s)
  128. {
  129.     Str255        temp;
  130.     
  131.     NumToString((long)aRect->top,s);
  132.     PStrCat(s,"\p,");
  133.     NumToString((long)aRect->left,temp);
  134.     PStrCat(s,temp);
  135.     PStrCat(s,"\p,");
  136.     NumToString((long)aRect->bottom,temp);
  137.     PStrCat(s,temp);
  138.     PStrCat(s,"\p,");
  139.     NumToString((long)aRect->right,temp);
  140.     PStrCat(s,temp);
  141. }
  142.  
  143. Boolean    isPressed(unsigned short k)
  144. {
  145.     unsigned char    km[16];
  146.     
  147.     GetKeys((long *)km);
  148.     return(Boolean)( (km[ k>>3 ] >> (k & 7) ) & 1);
  149.  
  150. }
  151.